package org.example.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.enums.ResponseCodeEnum;

import java.io.Serializable;

/**
 * @author: wf
 * @create: 2020/10/23 13:38
 * @description: 接口请求公共返回类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class APIResponse<T> implements Serializable {
    private static final long serialVersionUID = -877581211971197127L;
    /**
     * 请求状态
     */
    private Integer code;

    /**
     * 状态信息
     */
    private String msg;

    /**
     * 业务数据
     */
    private T data;

    private APIResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    private APIResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 请求成功,返回data数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> APIResponse<T> success(T data) {
        return new APIResponse<>(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMessage(), data);
    }
    public static <T> APIResponse<T> success() {
        return new APIResponse<>(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMessage());
    }

    /**
     * 请求失败处理
     * @param <T>
     * @return
     */
    public static <T> APIResponse<T> error() {
        return new APIResponse<>(ResponseCodeEnum.UNKNOWN_ERROR.getCode(), ResponseCodeEnum.UNKNOWN_ERROR.getMessage());
    }
    public static <T> APIResponse<T> error(String msg) {
        return new APIResponse<>(ResponseCodeEnum.UNKNOWN_ERROR.getCode(), msg);
    }
    public static <T> APIResponse<T> error(int code, String msg) {
        return new APIResponse<>(code, msg);
    }
    public static <T> APIResponse<T> error(ResponseCodeEnum codeEnum) {
        return new APIResponse<>(codeEnum.getCode(), codeEnum.getMessage());
    }
    public static <T> APIResponse<T> error(ResponseCodeEnum codeEnum, String msg) {
        return new APIResponse<>(codeEnum.getCode(), msg);
    }
    //请求接口失败,返回默认数据
    public static <T> APIResponse<T> error(ResponseCodeEnum codeEnum, String msg, T defaultData) {
        return new APIResponse<>(codeEnum.getCode(), msg, defaultData);
    }
}

